﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlServerCe;

namespace CPTECH.Repository
{
    class DepartamentoDB: BaseDB
    {
        private static DepartamentoDB instance;

        private DepartamentoDB() { }

        public static DepartamentoDB getInstance()
        {
            if (instance == null)
            {
                instance = new DepartamentoDB();
            }
            return instance;
        }

        //retorna todos os departamentos da base.
        public ArrayList selectAllDepartamento()
        {
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select cd_fis_depto, ds_depto from TB_depto;", this.conn);
                SqlCeDataReader rdr = command.ExecuteReader();
                ArrayList result = new ArrayList();

                while (rdr.Read())
                {
                    result.Add(rdr.GetInt64(0).ToString() + " = " + rdr.GetString(1));
                }
                return result;
            }
            finally
            {
                this.close();
            }
        }

        //checa se código departamento já existe.
        public bool checkCodDepartamento(Int64 codDepartamento)
        {
            bool result = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select * from TB_DEPTO where cd_fis_depto =@codDepto;", this.conn);
                command.Parameters.AddWithValue("@codDepto", codDepartamento);
                SqlCeDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    result = true;
                }
            }
            finally
            {
                this.close();
            }
            return result;
        }

        //Adiciona novo Departamento.
        public bool insertNewDepto(string os, Int64 codigoDepto, string descDepto, string dtRegistro, byte enviado)
        {
            bool resposta = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("insert into TB_DEPTO values('" + os + "','" + codigoDepto + "', '" + descDepto +
                                                        "','" + dtRegistro + "','" + enviado + "');", this.conn);
                command.ExecuteNonQuery();
                resposta = true;
            }
            catch (FormatException)
            {
                resposta = false;
            }
            finally
            {
                this.close();
            }
            return resposta;
        }
    }
}

